package com.erp.service;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;

import com.erp.dao.ErpUserDao;
import com.erp.data.ErpbaseUser;
import com.erp.util.SessionManagerUtil;

@Service("loginService")
@RemotingDestination(channels = { "my-amf" })
public class LoginService {

	@RemotingInclude
	public boolean login(ErpbaseUser erpbaseUser) {

		boolean result = false;

		ErpUserDao erpUserDao = new ErpUserDao();
		ErpbaseUser user = erpUserDao.findById(erpbaseUser.getUserId());
		if (user != null) {
			if (erpbaseUser.getUserPass().equals(user.getUserPass())) {
				result = true;
			}
		}
		if (result) {
			SessionManagerUtil.setLoginUserInfo(user);
		} else {
			SessionManagerUtil.setLoginUserInfo(null);
		}

		System.out.println("1111111111111111");
		System.out.println(result);

		return result;
	}

	@RemotingInclude
	public String isLogin() {
		ErpbaseUser loginUser = SessionManagerUtil.getLoginUserInfo();
		System.out.println("2222222222222222222222");
		System.out.println(loginUser.getUserId());
		if (loginUser == null) {
			return "logout";
		} else {
			return "login";
		}
	}

	public Object[] createUser(String a) {

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", false);
		result.put("data", "");

		JSONArray jsonArray = new JSONArray();

		jsonArray = JSONArray.fromObject(result);

		return jsonArray.toArray();
	}
}
